package cn.stylefeng.guns.sys.modular.rest.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.LogUtil;
import cn.stylefeng.guns.sys.modular.rest.entity.NameRelation;
import cn.stylefeng.guns.sys.modular.rest.mapper.NameRelationMapper;
import cn.stylefeng.guns.sys.modular.rest.model.params.NameRelationParam;
import cn.stylefeng.guns.sys.modular.rest.model.result.NameRelationResult;
import  cn.stylefeng.guns.sys.modular.rest.service.NameRelationService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huylei
 * @since 2022-05-18
 */
@Service
public class NameRelationServiceImpl extends ServiceImpl<NameRelationMapper, NameRelation> implements NameRelationService {
    @Resource
    NameRelationMapper nameRelationMapper;

    @Override
    public void updateByScmCode(NameRelation nameRelation) {
        nameRelationMapper.updateByScmCode(nameRelation);
    }

    @Override
    public NameRelation findByShopCode(String shopCode) {
        return nameRelationMapper.findByShopCode(shopCode);
    }

    /**
     * 批量添加
     */
    @Override
    public void addListAll(List<NameRelation> list) {
        //System.out.println("+++++++++"+list.get(0));
        nameRelationMapper.addAllList(list);
    }

    @Override
    public void add(NameRelation nameRelation){
        //this.save(entity);
        nameRelationMapper.addOne(nameRelation);
        //this.nameRelationMapper.insert(nameRelation);

    }

    @Override
    public void delete(NameRelationParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NameRelationParam param){
        NameRelation oldEntity = getOldEntity(param);
        NameRelation newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NameRelationResult findBySpec(NameRelationParam param){
        return null;
    }

    @Override
    public List<NameRelationResult> findListBySpec(NameRelationParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NameRelationParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 获取店名
     * @return
     */
    @Override
    public List<Map<String, String>> getName() {
        List<Map<String, String>> list = this.baseMapper.getName();
        return list;
    }

    /**
     * storeIdList
     * @return
     */
    @Override
    public List<String> getStoreIdList() {
        List<String> list = this.baseMapper.getStoreIdList();
        return list;
    }

    private Serializable getKey(NameRelationParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NameRelation getOldEntity(NameRelationParam param) {
        return this.getById(getKey(param));
    }

    private NameRelation getEntity(NameRelationParam param) {
        NameRelation entity = new NameRelation();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
